import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import '../Robot.dart';
import '../details.dart';
import '../pojo.dart';

///户型选择
class AddQAProvider extends ChangeNotifier {
  final map = ["机器人招揽",  "商品介绍", "自定义问答", "地点引领播报", "操作指令"];

  String _qaTitle = "我是自定义问答标题";

  String get qaTitle => _qaTitle;

  String _qaTitle2 = "我是自定义问答对应内容";

  String get qaTitle2 => _qaTitle2;

  ///自定义语音配置的当前输入内容
  final contents = ["",];

  ///只在界面刷新之后调用
  void updateContent(int index, String value) {
    print("AddQAProvider updateContent: "+index.toString()+":"+value.toString());
    contents[index] = value;
  }

  void removeAt(int index) {
    contents.removeAt(index);
    notifyListeners();
  }

  List<String> get contentsList => contents;
  // String _selectedType = "机器人招揽";
  String _selectedType = "地点引领播报";

  String get selectedType => _selectedType;

  void setSelectedType(String type) {
    _selectedType = type;
    notifyListeners();
  }

  void addSize() {
    contents.add("");
    notifyListeners();
  }

  // Customer information fields
  String _customerName = "";
  String _phoneNumber = "";
  String _customerType = "";
  int _renderCount = 19;
  double _totalAmount = 2000.00;

  // Getter methods for each property
  String get customerName => _customerName;

  String get phoneNumber => _phoneNumber;

  String get customerType => _customerType;

  int get renderCount => _renderCount;

  double get totalAmount => _totalAmount;

  //  客户列表
  List<HouseLayoutItem> _list = [];

  List<HouseLayoutItem> get houseLayoutList => _list;

  bool _isLoading = false;

  bool get isLoading => _isLoading;
  final String name = "name";

  List<Item147> dataList2 = [];

  //界面只需要这个
  List<Item147> get list3 => dataList2;

  //户型类型
  List<SysDictType> data = [];

  Future<void> getSysDicByType({
    required String accessToken,
    required String type,
  }) async {
    print("getSysDicByType start");

    var url = Uri.https(host, 'dict/getSysDicByType/$type');
    final uri = url.replace(
      queryParameters: {"access_token": accessToken, "type": type},
    );

    // print("toJson: ${a}");
    final response = await http.get(
      uri,
      headers: {"Content-Type": "application/json; charset=UTF-8"},
    );

    if (response.statusCode == 200) {
      print("系统字典 response：${response.body}");
      Map<String, dynamic> jsonResponse2 = json.decode(response.body);
      HouseTypeResponse response2 = HouseTypeResponse.fromJson(jsonResponse2);
      data = response2.data;
      // Access parsed data
      for (var houseType in data) {
        print('Name: ${houseType.name}, Code: ${houseType.code}');
      }
      notifyListeners();
      return;
    } else {
      throw Exception('Failed to fetch products: ${response.reasonPhrase}');
    }
  }

  Future<void> getHouseLayouts({
    required String accessToken,
    required String userId,
    required String companyId,
  }) async {
    print("getHouseLayouts start");
    // _isLoading = true;
    // notifyListeners();

    // 创建 Condition3 对象
    Condition2 condition = Condition2();
    FetchDataRequest9<Condition2> request = FetchDataRequest9<Condition2>(
      condition: condition,
    );

    var url = Uri.https(host, 'housePlans/pageWithReadResult');
    final uri = url.replace(
      queryParameters: {
        "access_token": accessToken,
        "userId": userId,
        "companyId": companyId,
      },
    );

    final a = jsonEncode(request.toJson((condition) => condition.toJson()));
    // print("toJson: ${a}");
    final response = await http.post(
      uri,
      headers: {"Content-Type": "application/json; charset=UTF-8"},
      body: a,
    );

    if (response.statusCode == 200) {
      print("户型数据  response：${response.body}");
      final Map<String, dynamic> jsonResponse = json.decode(response.body);
      _isLoading = false;
      // print("getCompanyProduct end");
      var a = ApiResponse<HouseLayoutData>.fromJson(
        jsonResponse,
        (data) => HouseLayoutData.fromJson(data),
      );
      _list = a.data!.list;
      notifyListeners();
      return;
    } else {
      throw Exception('Failed to fetch products: ${response.reasonPhrase}');
    }
  }

  Future<bool> login() async {
    print("login start");
    var url = Uri.https(host, 'robot/login');
    var response = await http.post(url, body: {'sn': Robot.sn});
    var a = ApiResponse<LoginBean>.fromJson(
      jsonDecode(response.body),
      (data) => LoginBean.fromJson(data),
    ); // 传递一个从 JSON 解析 LoginBean 的方法);

    print(a.data.toString());
    if (a.data?.userId == null) {
      print('请扫码');
      return false;
    }
    Robot.userId = a.data!.userId!;
    Robot.companyId = a.data!.companyId!;
    Robot.accessToken = a.data!.accessToken!;
    print("Robot.accessToken:" + Robot.accessToken);
    print("login end");
    return true;
  }
}

const fetchSize = 24;
